package com.easyadmin.pro.modules.sys.controller;

import com.easyadmin.pro.common.constants.HttpConstants;
import com.easyadmin.pro.common.controller.BaseController;
import com.easyadmin.pro.common.enums.HttpCodeEnum;
import com.easyadmin.pro.common.exception.BusinessException;
import com.easyadmin.pro.common.page.Pageable;
import com.easyadmin.pro.common.vo.RestResult;
import com.easyadmin.pro.modules.sys.entity.DataDictionaryEntity;
import com.easyadmin.pro.modules.sys.query.DataDictionaryQuery;
import com.easyadmin.pro.modules.sys.rest.DataDictionaryResult;
import com.easyadmin.pro.modules.sys.service.IDataDictionaryService;
import com.easyadmin.pro.modules.sys.vo.DataDictionaryVo;
import com.easyadmin.pro.modules.sys.vo.EnableOrDisableVo;
import com.easyadmin.pro.tool.DataDictionaryUtils;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 *  控制层。
 *
 * @author xulin
 * @since 2024-08-23
 */
@RestController
@RequestMapping(HttpConstants.SYS_MODULE_PATH + "/dataDictionary")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DataDictionaryController extends BaseController {

    private final IDataDictionaryService iDataDictionaryService;

    /**
     * 添加。
     *
     * @param dataDictionaryVo
     * @return
     */
    @PostMapping("saveOrUpdate")
    public RestResult saveOrUpdate(@RequestBody DataDictionaryVo dataDictionaryVo) {
        DataDictionaryEntity dataDictionaryEntity = new DataDictionaryEntity();
        BeanUtils.copyProperties(dataDictionaryVo, dataDictionaryEntity);
        return success(iDataDictionaryService.saveOrUpdate(dataDictionaryEntity));
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public RestResult remove(@PathVariable Serializable id) {
        return success(iDataDictionaryService.removeById(id));
    }

    /**
     * 根据编码删除
     * @param dataCode
     * @return
     */
    @DeleteMapping("removeData/{dataCode}")
    public RestResult remove(@PathVariable String dataCode) {
        return success(iDataDictionaryService.remove(new QueryWrapper()
                .eq(DataDictionaryEntity::getDataCode, dataCode)));
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public RestResult getInfo(@PathVariable Serializable id) {
        DataDictionaryResult dataDictionaryResult = new DataDictionaryResult();
        BeanUtils.copyProperties(iDataDictionaryService.getById(id), dataDictionaryResult);
        return success(dataDictionaryResult);
    }

    /**
     * 分页查询。
     *
     * @param dataDictionaryQuery
     * @return 分页对象
     */
    @GetMapping("page")
    public RestResult page(DataDictionaryQuery dataDictionaryQuery) {
        return success(Pageable.of(dataDictionaryQuery, () -> iDataDictionaryService.queryPage(dataDictionaryQuery)));
    }

    /**
     * 检查编码是否重复
     * @param dataCode
     * @return
     */
    @GetMapping("checkDataCode/{dataCode}")
    public RestResult checkDataCode(@PathVariable String dataCode) {
        long count = iDataDictionaryService.count(new QueryWrapper().eq(DataDictionaryEntity::getDataCode, dataCode));
        if (count > 0) {
            throw new BusinessException(HttpCodeEnum.DATA_CODE_EXISTS_ERROR);
        }
        return success();
    }

    /**
     * 启用/禁用
     * @param enableOrDisableVo
     * @return
     */
    @PostMapping("enableOrDisable")
    public RestResult enableOrDisable(@RequestBody @Validated EnableOrDisableVo enableOrDisableVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DataDictionaryEntity::getDataCode, enableOrDisableVo.getDataCode())
                .eq(DataDictionaryEntity::getId, enableOrDisableVo.getId());
        iDataDictionaryService.update(new DataDictionaryEntity()
                .setStatus(enableOrDisableVo.getStatus()), queryWrapper);
        return success();
    }

    /**
     * 刷新缓存
     * @return
     */
    @PutMapping("refreshCache")
    public RestResult refreshCache() {
        DataDictionaryUtils.refreshCache();
        return success();
    }

}
